package com.app.service.impl;

import com.app.common.EducationTool;
import com.app.mapper.jiuquan.MentorInfoMapper;
import com.app.mapper.jiuquan.MentorUserMapper;
import com.app.mapper.jiuquan.TbcConfigMapper;
import com.app.mapper.jiuquan.UserMapper;
import com.app.model.MentorInfoModel;
import com.app.model.MentorUserModel;
import com.app.model.TbcConfigModel;
import com.app.service.MentorInfoService;
import com.app.service.MentorUserService;
import com.app.service.TbcConfigService;
import com.app.util.CommonHelp;
import com.app.util.DateMaskingUtils;
import com.app.util.Result;
import com.app.util.StringTool;
import com.app.vo.EducationVo;
import com.app.vo.MentorInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//创业导师
@Service
public class MentorInfoServiceImpl extends ServiceImpl<MentorInfoMapper, MentorInfoModel> implements MentorInfoService {
    @Autowired
    private MentorInfoMapper mentorInfoMapper;

    @Autowired
    private TbcConfigMapper tbcConfigMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MentorUserMapper mentorUserMapper;

    //创业导师列表
    @Override
    public MentorInfoVo getMentorInfoList(Integer page, Integer limit, String name) {
        Page<MentorInfoModel> page1 = new Page<>(page, limit);
        QueryWrapper<MentorInfoModel>qw =new QueryWrapper<>();
        if (StringTool.isNotEmpty(name)){
            qw.like("name",name);
        }
        Page<MentorInfoModel> mentorInfoPage = mentorInfoMapper.selectPage(page1, qw);

        List<MentorInfoModel> mentorInfoList = mentorInfoPage.getRecords();
        for (MentorInfoModel mentorInfo : mentorInfoList) {
            String phone = mentorInfo.getPhone();
            String phoneMi = DateMaskingUtils.mobileEncrypt(phone);
            mentorInfo.setPhoneMi(phoneMi);
            String vx = mentorInfo.getVx();
            String vxMi = DateMaskingUtils.strMasking(vx, 2, 0);
            mentorInfo.setVxMi(vxMi);
        }
        long count = mentorInfoPage.getTotal();
        MentorInfoVo mentorInfoVo = new MentorInfoVo();
        mentorInfoVo.setList(mentorInfoList);
        mentorInfoVo.setCount((int) count);
        return mentorInfoVo;
    }

    @Override
    public MentorInfoVo getMentorInfoMajorList(Integer page, Integer limit, String major) {
        Page<MentorInfoModel> page1 = new Page<>(page, limit);
        QueryWrapper<MentorInfoModel>qw =new QueryWrapper<>();
        if (StringTool.isNotEmpty(major)){
            qw.like("major",major);
        }
        Page<MentorInfoModel> mentorInfoPage = mentorInfoMapper.selectPage(page1, qw);

        List<MentorInfoModel> mentorInfoList = mentorInfoPage.getRecords();
        for (MentorInfoModel mentorInfo : mentorInfoList) {
            String phone = mentorInfo.getPhone();
            String phoneMi = DateMaskingUtils.mobileEncrypt(phone);
            mentorInfo.setPhoneMi(phoneMi);
            String vx = mentorInfo.getVx();
            String vxMi = DateMaskingUtils.strMasking(vx, 2, 0);
            mentorInfo.setVxMi(vxMi);
        }
        long count = mentorInfoPage.getTotal();
        MentorInfoVo mentorInfoVo = new MentorInfoVo();
        mentorInfoVo.setList(mentorInfoList);
        mentorInfoVo.setCount((int) count);
        return mentorInfoVo;
    }

    //咨询导师(辅导次数+1)
    @Override
    public String addCounsellingCount(Integer mentorId, Integer userId) {
        QueryWrapper<MentorUserModel> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("mentor_id", mentorId);
        //查询此用户是否联系过这个导师
        MentorUserModel mentorUser = mentorUserMapper.selectOne(qw);
        //如果不是 null说明导师与用户已属于辅导关系
        if (mentorUser != null) {
            return "联系成功";
        }
        //如果是 null说明未联系，建立联系
        MentorUserModel mentorUserModel = new MentorUserModel();
        mentorUserModel.setMentorId(mentorId);
        mentorUserModel.setUserId(userId);
        mentorUserModel.setCreateTime(CommonHelp.getNowTimestamp());
        mentorUserMapper.insert(mentorUserModel);
        //将这个导师的辅导次数+1
        MentorInfoModel mentorInfo = mentorInfoMapper.selectById(mentorId);
        Integer counsellingCount = mentorInfo.getCounsellingCount();
        counsellingCount = counsellingCount + 1;
        mentorInfo.setCounsellingCount(counsellingCount);
        mentorInfoMapper.updateById(mentorInfo);
        return "联系成功";
    }
}
